package com.jolink.omiis.wechat.redis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.msgpack.MessagePack;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

/**
 * 
 * Redis单例操作
 * 
 * @author WangYanqing
 *
 */
public class RedisDefaultProcessor extends AbstractRedisProcessor implements IRedisProcessor {

	private static final Logger logger = LoggerFactory.getLogger(RedisClusterProcessor.class);

	@Override
	public Map<String, String> hgetall(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Map<String, String> value = jedis.hgetAll(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-getall操作成功，耗时：{}毫秒。", (t2 - t1));
		return value;
	}

	@Override
	public String hmset(String key, Map<String, String> row) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		String statusCode = jedis.hmset(key, row);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-hmset操作成功，耗时：{}毫秒。", (t2 - t1));
		return statusCode;
	}

	@Override
	public String hset(String key, String field, String value) throws Exception {
		Map<String, String> row = new HashMap<>();
		row.put(field, value);
		return hmset(key, row);
	}

	@Override
	public boolean exists(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Boolean flag = jedis.exists(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-exists操作成功，耗时：{}毫秒。", (t2 - t1));
		return flag.booleanValue();
	}

	@Override
	public String expireKey(String key, int seconds) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.expire(key, seconds);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-expireKey操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public String set(String key, String value) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.append(key, value);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-set操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public String get(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		String value = jedis.get(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-get操作成功，耗时：{}毫秒。", (t2 - t1));
		return value;
	}

	@Override
	public List<String> blpop(int timeout, String... keys) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		List<String> values = jedis.blpop(timeout, keys);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-blpop操作成功，耗时：{}毫秒。", (t2 - t1));
		return values;
	}

	@Override
	public List<String> brpop(int timeout, String... keys) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		List<String> values = jedis.brpop(timeout, keys);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-brpop操作成功，耗时：{}毫秒。", (t2 - t1));
		return values;
	}

	@Override
	public String rpop(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		String value = jedis.rpop(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-rpop操作成功，耗时：{}毫秒。", (t2 - t1));
		return value;
	}

	@Override
	public String lpop(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		String value = jedis.lpop(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-lpop操作成功，耗时：{}毫秒。", (t2 - t1));
		return value;
	}

	@Override
	public String llen(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long len = jedis.llen(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行redis-llen操作成功，耗时：{}毫秒。", t2 - t1);
		return String.valueOf(len);
	}

	@Override
	public String lpush(String key, String... values) {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.lpush(key, values);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-lpush操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public String lpush(String key, List<String> values) throws Exception {
		return lpush(key, values.toArray(new String[] {}));
	}

	@Override
	public String rpush(String key, String... values) {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.rpush(key, values);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-rpush操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public String sadd(String key, String... values) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.rpush(key, values);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-rpush操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public String scard(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.scard(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-scard操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public Set<String> smembers(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Set<String> set = jedis.smembers(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-smember操作成功，耗时：{}毫秒。", (t2 - t1));
		return set;
	}

	@Override
	public String spop(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		String value = jedis.spop(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-spop操作成功，耗时：{}毫秒。", (t2 - t1));
		return value;
	}

	@Override
	public String zadd(String key, Map<String, Double> values) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long statusCode = jedis.zadd(key, values);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-zadd操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(statusCode);
	}

	@Override
	public String zcard(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long len = jedis.zcard(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-zcard操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(len);
	}

	@Override
	public <T> String set(String key, T bean) throws Exception {
		long t1 = System.currentTimeMillis();
		MessagePack mp = new MessagePack();
		mp.register(bean.getClass());
		byte[] contents = mp.write(bean);
		Jedis jedis = getJedisResources();
		String statusCode = jedis.set(key.getBytes(), contents);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-set操作成功，耗时：{}毫秒。", (t2 - t1));
		return statusCode;
	}

	@Override
	public <T> T get(String key, Class<T> type) throws Exception {
		long t1 = System.currentTimeMillis();
		MessagePack mp = new MessagePack();
		Jedis jedis = getJedisResources();
		byte[] contents = jedis.get(key.getBytes());
		mp.register(type);
		T bean = mp.read(contents, type);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-get操作成功，耗时：{}毫秒。", (t2 - t1));
		return bean;
	}

	@Override
	public String[] keys(String keyPattern) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Set<String> set = jedis.keys(keyPattern);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-keys操作成功，耗时：{}毫秒。", (t2 - t1));
		return set.toArray(new String[] {});
	}

	@Override
	public String lrem(String key, String value) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long result = jedis.lrem(key, 0, value);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-lrem操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(result);
	}

	@Override
	public String incr(String key) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long result = jedis.incr(key);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-incr操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(result);
	}

	@Override
	public String hdel(String key, String... fields) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long result = jedis.hdel(key, fields);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-hdel操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(result);
	}

	@Override
	public String del(String... keys) throws Exception {
		long t1 = System.currentTimeMillis();
		Jedis jedis = getJedisResources();
		Long result = jedis.del(keys);
		long t2 = System.currentTimeMillis();
		close(jedis);
		logger.info("执行Redis-del操作成功，耗时：{}毫秒。", (t2 - t1));
		return String.valueOf(result);
	}

	@Override
	public void subscribe(JedisPubSub listener, String... channels) throws Exception {
	}

	@Override
	public String srem(String key, String... fields) throws Exception {
		return null;
	}

	@Override
	public String hmset(String key, String... fields) throws Exception {
		return null;
	}
}
